<?
/*
 * Friends Only Feed (FOFeed)
 *
 * Copyright 2011 Andrey Esin <gmlastik@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/

require_once 'config.php';
require_once 'mood_factory.php';

function download($friend_name, $user_name, $user_password) {
    global $script_user_agent, $use_gzip;
    $url = "http://".$friend_name.".livejournal.com/data/rss?auth=digest";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $script_user_agent);
    curl_setopt($ch, CURLOPT_USERPWD, $user_name.":".$user_password);
    if ($use_gzip) curl_setopt($ch, CURLOPT_ENCODING , "gzip");
    $xmlResponse = curl_exec($ch);
    echo curl_error($ch);
//* @return string the error number or '' (the empty string) if no error occurred.
    curl_close($ch);

    return $xmlResponse;
}

function save_to_file($xml_data, $friend_name, $path) {
    $file_name = $friend_name.".xml";
    $fh = fopen($path.$file_name, 'w');
    if (!$fh) return false;
    ftruncate($fh, 0);
    fwrite($fh, $xml_data);
    fclose($fh);
}

function check_friend_name($friend_name) {
    if (strlen($friend_name))
        return str_replace('_', '-', $friend_name);
    return '';
}

function add_mood_to_description($entry) {
    $namespaces = $entry->getNameSpaces(true);
    $post_text = $entry->description;
    $lj = $entry->children($namespaces['lj']);
    $mood_string = (string)$lj->mood;
    if (!$mood_string) return;
    $post_text = $post_text."<br><b>Current mood: </b>";
    $mood_link = get_link_by_mood($mood_string);
    if ($mood_link != $mood_string)
        $post_text = $post_text."<img src=".$mood_link." alt=".$mood_string."/>";
    $post_text = $post_text.$mood_string."</br>";
    $entry->description = $post_text;
}

function add_music_to_description($entry) {
    $namespaces = $entry->getNameSpaces(true);
    $post_text = $entry->description;
    $lj = $entry->children($namespaces['lj']);
    $music_string = (string)$lj->music;
    if (!$music_string) return;
    $post_text = $post_text."<br><b>Current music: </b>".$music_string;
    $post_text = $post_text."</br>";
    $entry->description = $post_text;

    //Add Last.fm
}

function parse_xml($raw) {
    $xml = new SimpleXmlElement($raw);
    foreach ($xml->channel->item as $entry) {
        add_mood_to_description($entry);
        add_music_to_description($entry);
    }
    return $xml->asXML();
}
foreach ($friend_list as $friend_name) {
    $resp = download(check_friend_name($friend_name), $user_name, $user_password);
    $resp = parse_xml($resp);
    save_to_file($resp, $friend_name, $path_to_save);
}

?>
